import numpy as np
from sklearn.svm import SVC
from sklearn.multiclass import OneVsRestClassifier
from sklearn import metrics
import matplotlib.pyplot as plt


def do_svm(text_vectors, labels, confusion_matrix_path, kernel='poly'):
    model = OneVsRestClassifier(SVC(kernel=kernel))
    clf = model.fit(text_vectors, labels)

    predicted_labels = clf.predict(text_vectors)
    print(metrics.classification_report(labels, predicted_labels))

    C = metrics.confusion_matrix(labels, predicted_labels)

    plt.matshow(C, cmap=plt.cm.Reds)
    for i in range(len(C)):
        for j in range(len(C)):
            plt.annotate(C[j, i], xy=(i, j), horizontalalignment='center', verticalalignment='center')

    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    plt.savefig(confusion_matrix_path)
    plt.show()
    return clf
